﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using zoyobar.shared.panzer.web.ib;
using System.Net;
using demo.Properties;

namespace demo
{
    public partial class Form1 : Form
    {
        IEBrowser ie;

        private string GetUrl(string path) {
            return Path.Combine(Settings.Default.WcfHost, path);
        }

        private void ParseOperator(string url)
        {
            HttpWebRequest client = (HttpWebRequest)WebRequest.Create(GetUrl(url));

            HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
            doc.Load(client.GetResponse().GetResponseStream());
        }

        public Form1()
        {
            InitializeComponent();
            ie = new IEBrowser(this.wb);
        }

        private Uri GetScriptUri(string script)
        {
            return new Uri(Path.Combine(Application.StartupPath, "js", script));
        }

        private string GetScriptContent(string script)
        {
            return File.ReadAllText(Path.Combine(Application.StartupPath, "js", script));
            //return Uri.EscapeDataString(uri.AbsoluteUri);
        }

        private void InstallScript()
        {

        }

        private void btnInstallScript_Click(object sender, EventArgs e)
        {            
            ie.InstallScript(GetScriptContent("wcfclient.js"));
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            wb.Navigate("about:blank");
        }

        private void btnLoad_Click(object sender, EventArgs e)
        {
            Cursor = Cursors.AppStarting;
            try
            {
                wb.Navigate(Settings.Default.WcfHost);

                while (wb.ReadyState != WebBrowserReadyState.Complete)
                {
                    Application.DoEvents();
                }

                ie.InstallTrace();
                ie.InstallJQuery(GetScriptContent("jquery.js"));
                ie.InstallJQuery(GetScriptContent("json2.js"));
            }
            finally
            {
                Cursor = Cursors.Default;
            }
        }

        private void btnHAP_Click(object sender, EventArgs e)
        {
            HttpWebRequest client = (HttpWebRequest)WebRequest.Create(GetUrl("help"));

            HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
            doc.Load(client.GetResponse().GetResponseStream());
            doc.DocumentNode.SelectNodes("//div[@id='content']/table/tr")
                .Skip(1)
                .ForEach(tr => {
                    var td = tr.SelectNodes("td");
                    var _operator = td[1].SelectSingleNode("a");
                    ParseOperator(_operator.GetAttributeValue("href", string.Empty));
                });
        }
    }
}
